package com.yealove.lunchparty.service;

import com.yealove.common.utils.DictItem;
import com.yealove.lunchparty.bean.*;
import com.yealove.lunchparty.mapper.ILunchPartyMapper;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * User: yealove
 * Date: 8/21/14
 * Time: 12:32 AM
 * Created with IntelliJ IDEA.
 */
public class LunchpartyService {
    private ILunchPartyMapper lunchPartyMapper;

    public User login(User user) {
        return lunchPartyMapper.login(user);
    }

    public void reg(User user) {
        lunchPartyMapper.addUser(user);
    }

    /**
     * 获取今日菜单总数
     *
     * @return
     */
    public int getTodayMenuCount(String shopId) {
        return lunchPartyMapper.getTodayMenuCount(shopId);
    }

    /**
     * 获取今日菜单
     *
     * @return
     */
    public List<Menu> getTodayMenu(String shopId) {
        return lunchPartyMapper.getTodayMenu(shopId);
    }

    /**
     * 获取今日菜单
     *
     * @return
     */
    public <T extends PageBean> List<Menu> getTodayMenu(T t) {
        return lunchPartyMapper.getTodayMenuPage(t);
    }

    /**
     * 获取当前订单列表（4小时以内）
     *
     * @return
     */
    public List<Order> getTodayOrder() {
        return lunchPartyMapper.getTodayOrder();
    }

    /**
     * 获取当前订单列表（4小时以内）
     *
     * @return
     */
    public Order getOrderById(String id) {
        return lunchPartyMapper.getOrderById(id);
    }

    /**
     * 添加订单
     *
     * @param order
     * @return
     */
    public void addOrder(Order order, DictItem dictItem) {
        lunchPartyMapper.updateMoney(dictItem);
        lunchPartyMapper.addOrder(order);
    }

    /**
     * 统计
     *
     * @return
     */
    public List<DictItem> calc() {
        return lunchPartyMapper.calc();
    }

    /**
     * 更新余额
     *
     * @param dictItem dictid: userId   dictname: money
     */
    public void updateMoney(DictItem dictItem) {
        lunchPartyMapper.updateMoney(dictItem);
    }

    /**
     * 判断是否已经注册
     *
     * @param user
     * @return
     */
    public Boolean isReg(User user) {
        User u = lunchPartyMapper.isReg(user);
        if (u == null) {
            return false;
        }
        return true;
    }

    /**
     * 获取用户列表
     *
     * @return
     */
    public List<User> getUsers() {
        return lunchPartyMapper.getUsers();
    }

    /**
     * 获取用户信息
     *
     * @param id
     * @return
     */
    public User getUserById(String id) {
        return lunchPartyMapper.getUserById(id);
    }

    /**
     * 最近订单
     *
     * @param dictItem
     * @return
     */
    public List<Order> getRecentOrder(DictItem dictItem) {
        return lunchPartyMapper.getRecentOrder(dictItem);
    }

    /**
     * 获取权限信息
     *
     * @param key
     * @return
     */
    public Auth getAuthByKey(String key) {
        return lunchPartyMapper.getAuthByKey(key);
    }

    /**
     * 获取权限信息
     *
     * @return
     */
    public List<Auth> getAuths() {
        return lunchPartyMapper.getAuths();
    }

    /**
     * 退订
     *
     * @param id
     * @param dictItem
     */
    public void delOrderById(String id, DictItem dictItem) {
        lunchPartyMapper.delOrderById(id);
        lunchPartyMapper.updateMoney(dictItem);
    }

    /**
     * 更改权限值
     *
     * @param dictItem
     */
    public void updateAuth(DictItem dictItem) {
        lunchPartyMapper.updateAuth(dictItem);
    }

    /**
     * 获取商家列表
     *
     * @return
     */
    public List<Shop> getShops() {
        return lunchPartyMapper.getShops();
    }

    /**
     * 添加商家
     *
     * @param shop
     */
    public void addShop(Shop shop) {
        try {
            shop.setName(URLDecoder.decode(shop.getName(), "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        lunchPartyMapper.addShop(shop);
    }

    /**
     * 查询餐厅的所有的菜单
     *
     * @param shopId
     * @return
     */
    public List<Menu> getShopMenus(String shopId) {
        return lunchPartyMapper.getShopMenus(shopId);
    }

    /**
     * 添加新菜
     * @param menu
     */
    public void addMenu(Menu menu) {
        try {
            menu.setName(URLDecoder.decode(menu.getName(), "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        lunchPartyMapper.addMenu(menu);
    }

    public Shop qryDefaultShop(){
        return lunchPartyMapper.qryDefaultShop();
    }

    public void updateDefaultShop(String id){
        lunchPartyMapper.updateDefaultShopOne();
        lunchPartyMapper.updateDefaultShop(id);
    }

    public void delMenuById(String id) {
        lunchPartyMapper.delMenuById(id);
    }

    public ILunchPartyMapper getLunchPartyMapper() {
        return lunchPartyMapper;
    }

    public void setLunchPartyMapper(ILunchPartyMapper lunchPartyMapper) {
        this.lunchPartyMapper = lunchPartyMapper;
    }
}
